

///////////
// Setup //
///////////

log close _all
log using "2_Manuscript_Tables.smcl", replace


/////////////////////////////////////////////////////////////////
// Table 1: Summary Statistics for Treatment and Control Group //
/////////////////////////////////////////////////////////////////

preserve

// Estimation Sample

quietly reg satisfaction_life treatment_a $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(balancedness == 1), robust
keep if(e(sample) == 1)

// Simple Differences in Means
//////////////////////////////

// Variables

tabulate age, generate(age_)
tabulate gender, generate(gender_)
tabulate ethnicity, generate(ethnicity_)
tabulate religion, generate(religion_)
tabulate condition, generate(condition_)
tabulate isolating, generate(isolating_)
tabulate employment, generate(employment_)
tabulate volunteered_before, generate(volunteered_before_)
tabulate volunteered_elsewhere, generate(volunteered_elsewhere_)
tabulate volunteered_elsewhere_where, generate(vol_elsewhere_where_)

eststo: estpost ttest age_1 age_2 age_3 age_4 age_5 age_6 age_7 age_8 gender_1 gender_2 gender_3 gender_4 ethnicity_1 ethnicity_2 ethnicity_3 ethnicity_4 ethnicity_5 ethnicity_6 ethnicity_7 ethnicity_8 ethnicity_9 ethnicity_10 ethnicity_11 ethnicity_12 ethnicity_13 ethnicity_14 ethnicity_15 ethnicity_16 ethnicity_17 ethnicity_18 religion_1 religion_2 religion_3 religion_4 religion_5 religion_6 religion_7 religion_8 religion_9 condition_1 condition_2 condition_3 condition_4 isolating_1 isolating_2 isolating_3 employment_1 employment_2 employment_3 employment_4 employment_5 employment_6 employment_7 employment_8 employment_9 motivations2_1 motivations2_2 motivations2_3 motivations2_4 motivations2_5 motivations2_6 motivations2_7 motivations2_8 motivations2_9 motivations2_10 volunteered_before_1 volunteered_before_2 volunteered_elsewhere_1 volunteered_elsewhere_2 volunteered_elsewhere_3  vol_elsewhere_where_2 vol_elsewhere_where_3 vol_elsewhere_where_4 vol_elsewhere_where_5 vol_elsewhere_where_6 vol_elsewhere_where_7 vol_elsewhere_where_8 newAdmissions cumAdmissions hospitalCases covidOccupiedMVBeds newCasesBySpecimenDate cumCasesBySpecimenDate newDeaths28DaysByDeathDate cumDeaths28DaysByDeathDate, by(treatment_a)

esttab est1 using "Table_1", rtf label replace modelwidth(4) ///
title(Differences in Means: C vs. T) mtitles ///
cells("mu_1(fmt(4)) mu_2(fmt(4)) b(star f(4)) p(fmt(4))") ///
starlevels(* 0.10 ** 0.05 *** 0.01)
eststo clear

// Normalised Differences
/////////////////////////

global variables "age_1 age_2 age_3 age_4 age_5 age_6 age_7 age_8 gender_1 gender_2 gender_3 gender_4 ethnicity_1 ethnicity_2 ethnicity_3 ethnicity_4 ethnicity_5 ethnicity_6 ethnicity_7 ethnicity_8 ethnicity_9 ethnicity_10 ethnicity_11 ethnicity_12 ethnicity_13 ethnicity_14 ethnicity_15 ethnicity_16 ethnicity_17 ethnicity_18 religion_1 religion_2 religion_3 religion_4 religion_5 religion_6 religion_7 religion_8 religion_9 condition_1 condition_2 condition_3 condition_4 isolating_1 isolating_2 isolating_3 employment_1 employment_2 employment_3 employment_4 employment_5 employment_6 employment_7 employment_8 employment_9 motivations2_1 motivations2_2 motivations2_3 motivations2_4 motivations2_5 motivations2_6 motivations2_7 motivations2_8 motivations2_9 motivations2_10 volunteered_before_1 volunteered_before_2 volunteered_elsewhere_1 volunteered_elsewhere_2 volunteered_elsewhere_3  vol_elsewhere_where_2 vol_elsewhere_where_3 vol_elsewhere_where_4 vol_elsewhere_where_5 vol_elsewhere_where_6 vol_elsewhere_where_7 vol_elsewhere_where_8 newAdmissions cumAdmissions hospitalCases covidOccupiedMVBeds newCasesBySpecimenDate cumCasesBySpecimenDate newDeaths28DaysByDeathDate cumDeaths28DaysByDeathDate"

display "Means of Control Group"

foreach m of global variables {
	
	quietly sum `m' if(treatment_a == 0)
	
	scalar `m'_t0_m = r(mean)
	
	display `m'_t0_m
	
}	

display "Means of Treatment Group"

foreach m of global variables {
	
	quietly sum `m' if(treatment_a == 1)
	
	scalar `m'_t1_m = r(mean)
	
	display `m'_t1_m
	
}	

display "Standard Deviations of Control Group"

foreach m of global variables {
	
	quietly sum `m' if(treatment_a == 0)
	
	scalar `m'_t0_v = r(Var)
	
	display sqrt(`m'_t0_v)
	
}	

display "Standard Deviations of Treatment Group"

foreach m of global variables {
	
	quietly sum `m' if(treatment_a == 1)
	
	scalar `m'_t1_v = r(Var)
	
	display sqrt(`m'_t1_v)
	
}	

display "Normalised Differences"

foreach m of global variables {
	
	scalar `m'_nd = abs((`m'_t1_m - `m'_t0_m)/sqrt(`m'_t1_v + `m'_t0_v))
	
	display `m'_nd
	
}	

restore


////////////////////////////////////////////////////////////////
// Table 2: Universe of Volunteers vs. Main Estimation Sample //
////////////////////////////////////////////////////////////////

preserve

// Administrative Data

import delimited "Admin_Data_Universe.csv", delimiter("|") clear

// Simple Differences in Means
//////////////////////////////

replace finalsample = . if(finalsample == 0 & numberdaysonduty == 0)

// Variables

replace averagedistancetotasks = subinstr(averagedistancetotasks, ",", "", .) 
destring averagedistancetotasks, replace

generate service_1 = 0
replace service_1 = 1 if(tier == 10 | tier == 13 | tier == 14 | tier == 15 | tier == 22 | tier == 23 | tier == 30 | tier == 31 | tier == 35 | tier == 51 | tier == 55 | tier == 56 | tier == 63 | tier == 64 | tier == 65 | tier == 66 | tier == 68 | tier == 70 | tier == 4 | tier == 7 | tier == 9 | tier == 12 | tier == 19 | tier == 21 | tier == 27 | tier == 29 | tier == 34 | tier == 36 | tier == 41 | tier == 42 | tier == 46 | tier == 47 | tier == 53 | tier == 57 | tier == 58 | tier == 61)

generate service_2 = 0
replace service_2 = 1 if(tier == 16 | tier == 17 | tier == 18 | tier == 19 | tier == 20 | tier == 21 | tier == 22 | tier == 23 | tier == 32 | tier == 33 | tier == 34 | tier == 35 | tier == 46 | tier == 48 | tier == 52 | tier == 55 | tier == 57 | tier == 59 | tier == 61 | tier == 62 | tier == 64 | tier == 66 | tier == 69 | tier == 71 | tier == 2 | tier == 5 | tier == 8 | tier == 9 | tier == 11 | tier == 12 | tier == 14 | tier == 15 | tier == 25 | tier == 28 | tier == 29 | tier == 31 | tier == 38 | tier == 41 | tier == 43 | tier == 45 | tier == 50 | tier == 51 | tier == 53 | tier == 54 | tier == 58 | tier == 60 | tier == 63 | tier == 65)

generate service_12 = 0
replace service_12 = 1 if((tier == 10 | tier == 13 | tier == 14 | tier == 15 | tier == 22 | tier == 23 | tier == 30 | tier == 31 | tier == 35 | tier == 51 | tier == 55 | tier == 56 | tier == 63 | tier == 64 | tier == 65 | tier == 66 | tier == 68 | tier == 70 | tier == 4 | tier == 7 | tier == 9 | tier == 12 | tier == 19 | tier == 21 | tier == 27 | tier == 29 | tier == 34 | tier == 36 | tier == 41 | tier == 42 | tier == 46 | tier == 47 | tier == 53 | tier == 57 | tier == 58 | tier == 61) | (tier == 16 | tier == 17 | tier == 18 | tier == 19 | tier == 20 | tier == 21 | tier == 22 | tier == 23 | tier == 32 | tier == 33 | tier == 34 | tier == 35 | tier == 46 | tier == 48 | tier == 52 | tier == 55 | tier == 57 | tier == 59 | tier == 61 | tier == 62 | tier == 64 | tier == 66 | tier == 69 | tier == 71 | tier == 2 | tier == 5 | tier == 8 | tier == 9 | tier == 11 | tier == 12 | tier == 14 | tier == 15 | tier == 25 | tier == 28 | tier == 29 | tier == 31 | tier == 38 | tier == 41 | tier == 43 | tier == 45 | tier == 50 | tier == 51 | tier == 53 | tier == 54 | tier == 58 | tier == 60 | tier == 63 | tier == 65))

generate service_3 = 0
replace service_3 = 1 if(tier == 24 | tier == 25 | tier == 26 | tier == 27 | tier == 28 | tier == 29 | tier == 30 | tier == 31 | tier == 32 | tier == 33 | tier == 34 | tier == 35 | tier == 40 | tier == 47 | tier == 49 | tier == 50 | tier == 52 | tier == 56 | tier == 58 | tier == 60 | tier == 61 | tier == 62 | tier == 65 | tier == 66 | tier == 1 | tier == 5 | tier == 6 | tier == 7 | tier == 11 | tier == 12 | tier == 13 | tier == 15 | tier == 17 | tier == 20 | tier == 21 | tier == 23 | tier == 39 | tier == 42 | tier == 44 | tier == 45 | tier == 53 | tier == 54 | tier == 57 | tier == 59 | tier == 63 | tier == 64 | tier == 69 | tier == 70)

generate date_birth = date(dateofbirth, "DMY")
format date_birth %td
generate year_birth = year(date_birth)

generate age = 2020 - year_birth
replace age = . if(age < 18)

generate age_group = .
replace age_group = 1 if(age <= 24 & age != .)
replace age_group = 2 if(age >= 25 & age <= 34)
replace age_group = 3 if(age >= 35 & age <= 44)
replace age_group = 4 if(age >= 45 & age <= 54)
replace age_group = 5 if(age >= 55 & age <= 64)
replace age_group = 6 if(age >= 65 & age <= 74)
replace age_group = 7 if(age >= 75 & age != .)

tabulate age_group, generate(age_group_)

generate numbertasksnotified = numbertasksnotifiedtransport + numbertasksnotifiedcommunityresp + numbertasksnotifiedcheckinandcha

rename numbertasksnotifiedtransport numbertasksnotifiedtrans
rename numbertasksnotifiedcommunityresp numbertasksnotifiedcom
rename numbertasksnotifiedcheckinandcha numbertasksnotifiedcheck

generate treatment_a = .
replace treatment_a = 1 if((allocatedtask == 1 & acceptedtask == 1 & completedtask == 1) & finalsample == 1)
replace treatment_a = 0 if((allocatedtask == 0 | (allocatedtask == 1 & acceptedtask == 0 & numbertimedouttasks > 0)) & finalsample == 1)
replace treatment_a = 1 if((allocatedtask == 1) & finalsample == 0)
replace treatment_a = 0 if((allocatedtask == 0) & finalsample == 0)
tabulate treatment_a, generate(treatment_a_)

generate treatment_a_serv1 = .
replace treatment_a_serv1 = 1 if((allocatedtask == 1 & acceptedtask == 1 & completedtask == 1 & (service_1 == 1 | service_2 == 1) & (numbertasksnotifiedtrans > 0)) & finalsample == 1)
replace treatment_a_serv1 = 0 if(((allocatedtask == 0 | (allocatedtask == 1 & acceptedtask == 0 & numbertimedouttasks > 0)) & (service_1 == 1 | service_2 == 1)) & finalsample == 1)
replace treatment_a_serv1 = 1 if((allocatedtask == 1 & (service_1 == 1 | service_2 == 1) & (numbertasksnotifiedtrans > 0)) & finalsample == 0)
replace treatment_a_serv1 = 0 if(((allocatedtask == 0) & (service_1 == 1 | service_2 == 1)) & finalsample == 0)
tabulate treatment_a_serv1, generate(treatment_a_serv1_)

generate treatment_a_serv2 = .
replace treatment_a_serv2 = 1 if((allocatedtask == 1 & acceptedtask == 1 & completedtask == 1 & (service_1 == 1 | service_2 == 1) & (numbertasksnotifiedcom > 0)) & finalsample == 1)
replace treatment_a_serv2 = 0 if(((allocatedtask == 0 | (allocatedtask == 1 & acceptedtask == 0 & numbertimedouttasks > 0)) & (service_1 == 1 | service_2 == 1)) & finalsample == 1)
replace treatment_a_serv2 = 1 if((allocatedtask == 1 & (service_1 == 1 | service_2 == 1) & (numbertasksnotifiedcom > 0)) & finalsample == 0)
replace treatment_a_serv2 = 0 if(((allocatedtask == 0) & (service_1 == 1 | service_2 == 1)) & finalsample == 0)
tabulate treatment_a_serv2, generate(treatment_a_serv2_)

generate treatment_a_serv12 = .
replace treatment_a_serv12 = 1 if((allocatedtask == 1 & acceptedtask == 1 & completedtask == 1 & (service_1 == 1 | service_2 == 1) & (numbertasksnotifiedtrans > 0 | numbertasksnotifiedcom > 0)) & finalsample == 1)
replace treatment_a_serv12 = 0 if(((allocatedtask == 0 | (allocatedtask == 1 & acceptedtask == 0 & numbertimedouttasks > 0)) & (service_1 == 1 | service_2 == 1)) & finalsample == 1)
replace treatment_a_serv12 = 1 if((allocatedtask == 1 & (service_1 == 1 | service_2 == 1) & (numbertasksnotifiedtrans > 0 | numbertasksnotifiedcom > 0)) & finalsample == 0)
replace treatment_a_serv12 = 0 if(((allocatedtask == 0) & (service_1 == 1 | service_2 == 1)) & finalsample == 0)
tabulate treatment_a_serv12, generate(treatment_a_serv12_)

generate treatment_a_serv3 = .
replace treatment_a_serv3 = 1 if((allocatedtask == 1 & acceptedtask == 1 & completedtask == 1 & service_3 == 1 & numbertasksnotifiedcheck > 0) & finalsample == 1)
replace treatment_a_serv3 = 0 if(((allocatedtask == 0 | (allocatedtask == 1 & acceptedtask == 0 & numbertimedouttasks > 0)) & service_3 == 1) & finalsample == 1)
replace treatment_a_serv3 = 1 if((allocatedtask == 1 & service_3 == 1 & numbertasksnotifiedcheck > 0) & finalsample == 0)
replace treatment_a_serv3 = 0 if(((allocatedtask == 0) & service_3 == 1) & finalsample == 0)
tabulate treatment_a_serv3, generate(treatment_a_serv3_)

eststo: estpost ttest treatment_a_2 treatment_a_serv1_2 treatment_a_serv2_2 treatment_a_serv3_2 treatment_a_1 treatment_a_serv1_1 treatment_a_serv2_1 treatment_a_serv3_1 numbertasksnotified numbertasksnotifiedtrans numbertasksnotifiedcom numbertasksnotifiedcheck numberrejectedtasks numbertimedouttasks waitingtime service_1 service_2 service_3 age_group_1 age_group_2 age_group_3 age_group_4 age_group_5 age_group_6 age_group_7, by(finalsample)

esttab est1 using "Table_2", rtf label replace modelwidth(4) ///
title(Differences in Means: Universe vs. Estimation Sample) mtitles ///
cells("mu_1(fmt(4)) mu_2(fmt(4)) b(star f(4)) p(fmt(4))") ///
starlevels(* 0.10 ** 0.05 *** 0.01)
eststo clear

// Normalised Differences
/////////////////////////

global variables "treatment_a_2 treatment_a_serv1_2 treatment_a_serv2_2 treatment_a_serv3_2 treatment_a_1 treatment_a_serv1_1 treatment_a_serv2_1 treatment_a_serv3_1 numbertasksnotified numbertasksnotifiedtrans numbertasksnotifiedcom numbertasksnotifiedcheck numberrejectedtasks numbertimedouttasks waitingtime service_1 service_2 service_3 age_group_1 age_group_2 age_group_3 age_group_4 age_group_5 age_group_6 age_group_7"

display "Means of Universe"

foreach m of global variables {
	
	quietly sum `m' if(finalsample == 0)
	
	scalar `m'_t0_m = r(mean)
	
	display `m'_t0_m
	
}	

display "Means of Estimation Sample"

foreach m of global variables {
	
	quietly sum `m' if(finalsample == 1)
	
	scalar `m'_t1_m = r(mean)
	
	display `m'_t1_m
	
}	

display "Standard Deviations of Universe"

foreach m of global variables {
	
	quietly sum `m' if(finalsample == 0)
	
	scalar `m'_t0_v = r(Var)
	
	display sqrt(`m'_t0_v)
	
}	

display "Standard Deviations of Estimation Sample"

foreach m of global variables {
	
	quietly sum `m' if(finalsample == 1)
	
	scalar `m'_t1_v = r(Var)
	
	display sqrt(`m'_t1_v)
	
}	

display "Normalised Differences"

foreach m of global variables {
	
	scalar `m'_nd = abs((`m'_t1_m - `m'_t0_m)/sqrt(`m'_t1_v + `m'_t0_v))
	
	display `m'_nd
	
}

restore


////////////////////////////////////////
// Table 3: Average Treatment Effects //
////////////////////////////////////////

// Life Satisfaction

quietly reg satisfaction_life treatment_a $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(balancedness == 1), robust
generate marker = 1 if(e(sample))

reg satisfaction_life treatment_a                 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust
reg satisfaction_life treatment_a $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust

sum satisfaction_life if(marker == 1)
tab treatment_a if(marker == 1)

drop marker

// Sense of Purpose in Life

quietly reg worthwhileness_life treatment_a $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(balancedness == 1), robust
generate marker = 1 if(e(sample))

reg worthwhileness_life treatment_a                 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust
reg worthwhileness_life treatment_a $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust

sum worthwhileness_life if(marker == 1)
tab treatment_a if(marker == 1)

drop marker

// Belongingness

quietly reg belongingness treatment_a $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(balancedness == 1), robust
generate marker = 1 if(e(sample))

reg belongingness treatment_a                 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust
reg belongingness treatment_a $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust

sum belongingness if(marker == 1)
tab treatment_a if(marker == 1)

drop marker

// Connectedness

quietly reg connectedness treatment_a $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(balancedness == 1), robust
generate marker = 1 if(e(sample))

reg connectedness treatment_a                 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust
reg connectedness treatment_a $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust

sum connectedness if(marker == 1)
tab treatment_a if(marker == 1)

drop marker

// // Multiple Hypotheses Testing

rwolf sl wl b c if(balancedness == 1), indepvar(treatment_a) controls(                $y1 $y2 $z postcode_admin_aggregated2_*) method(regress) seed(123456789) reps(100) vce(cluster respondentid) cluster(respondentid)
rwolf sl wl b c if(balancedness == 1), indepvar(treatment_a) controls($x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_*) method(regress) seed(123456789) reps(100) vce(cluster respondentid) cluster(respondentid)

// // Lee (2009) Bounds

set seed 123456789

capture noisily leebounds satisfaction_life treatment_a, vce(bootstrap, reps(100)) cieffect tight(age2)
capture noisily leebounds worthwhileness_life treatment_a, vce(bootstrap, reps(100)) cieffect tight(age2)
capture noisily leebounds belongingness treatment_a, vce(bootstrap, reps(100)) cieffect tight(age2)
capture noisily leebounds connectedness treatment_a, vce(bootstrap, reps(100)) cieffect tight(age2)


//////////////////////////////////////////////////////////////////////////
// Table 4: Average Treatment Effect Intensity (% of Task Distribution) //
//////////////////////////////////////////////////////////////////////////

// Life Satisfaction

quietly reg satisfaction_life i.treatment_a_int $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(balancedness == 1), robust
generate marker = 1 if(e(sample))

reg satisfaction_life i.treatment_a_int                 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust
reg satisfaction_life i.treatment_a_int $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust

sum satisfaction_life if(marker == 1)
tab treatment_a_int if(marker == 1)

drop marker

// Sense of Purpose in Life

quietly reg worthwhileness_life i.treatment_a_int $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(balancedness == 1), robust
generate marker = 1 if(e(sample))

reg worthwhileness_life i.treatment_a_int                 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust
reg worthwhileness_life i.treatment_a_int $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust

sum worthwhileness_life if(marker == 1)
tab treatment_a_int if(marker == 1)

drop marker

// Belongingness

quietly reg belongingness i.treatment_a_int $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(balancedness == 1), robust
generate marker = 1 if(e(sample))

reg belongingness i.treatment_a_int                 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust
reg belongingness i.treatment_a_int $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust

sum belongingness if(marker == 1)
tab treatment_a_int if(marker == 1)

drop marker

// Connectedness

quietly reg connectedness i.treatment_a_int $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(balancedness == 1), robust
generate marker = 1 if(e(sample))

reg connectedness i.treatment_a_int                 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust
reg connectedness i.treatment_a_int $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust

sum connectedness if(marker == 1)
tab treatment_a_int if(marker == 1)

drop marker

// // Multiple Hypotheses Testing

tabulate treatment_a_int, generate(treatment_a_int_)

rename treatment_a_int_2 t_a_int_2
rename treatment_a_int_3 t_a_int_3
rename treatment_a_int_4 t_a_int_4
rename treatment_a_int_5 t_a_int_5
rename treatment_a_int_6 t_a_int_6
rename treatment_a_int_7 t_a_int_7

rwolf sl wl b c if(balancedness == 1), indepvar(t_a_int_2 t_a_int_3 t_a_int_4 t_a_int_5 t_a_int_6 t_a_int_7) controls(                $y1 $y2 $z postcode_admin_aggregated2_*) method(regress) seed(123456789) reps(100) vce(cluster respondentid) cluster(respondentid)
rwolf sl wl b c if(balancedness == 1), indepvar(t_a_int_2 t_a_int_3 t_a_int_4 t_a_int_5 t_a_int_6 t_a_int_7) controls($x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_*) method(regress) seed(123456789) reps(100) vce(cluster respondentid) cluster(respondentid)

drop treatment_a_int_1

drop t_a_int_2
drop t_a_int_3
drop t_a_int_4
drop t_a_int_5
drop t_a_int_6
drop t_a_int_7


//////////////////////////////////////////////////////////////
// Table 5: Heterogeneous Treatment Effects By Type of Task //
//////////////////////////////////////////////////////////////

// Transport and Community Response
///////////////////////////////////

// Life Satisfaction

quietly reg satisfaction_life treatment_a_service12 $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(balancedness == 1), robust
generate marker = 1 if(e(sample))

reg satisfaction_life treatment_a_service12                 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust
reg satisfaction_life treatment_a_service12 $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust

sum satisfaction_life if(marker == 1)
tab treatment_a_service12 if(marker == 1)

drop marker

// Sense of Purpose in Life

quietly reg worthwhileness_life treatment_a_service12 $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(balancedness == 1), robust
generate marker = 1 if(e(sample))

reg worthwhileness_life treatment_a_service12                 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust
reg worthwhileness_life treatment_a_service12 $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust

sum worthwhileness_life if(marker == 1)
tab treatment_a_service12 if(marker == 1)

drop marker

// Belongingness

quietly reg belongingness treatment_a_service12 $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(balancedness == 1), robust
generate marker = 1 if(e(sample))

reg belongingness treatment_a_service12                 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust
reg belongingness treatment_a_service12 $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust

sum belongingness if(marker == 1)
tab treatment_a_service12 if(marker == 1)

drop marker

// Connectedness

quietly reg connectedness treatment_a_service12 $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(balancedness == 1), robust
generate marker = 1 if(e(sample))

reg connectedness treatment_a_service12                 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust
reg connectedness treatment_a_service12 $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust

sum connectedness if(marker == 1)
tab treatment_a_service12 if(marker == 1)

drop marker

// // Multiple Hypotheses Testing

generate t_a_service12 = treatment_a_service12

rwolf sl wl b c if(balancedness == 1), indepvar(t_a_service12) controls(                $y1 $y2 $z postcode_admin_aggregated2_*) method(regress) seed(123456789) reps(100) vce(cluster respondentid) cluster(respondentid)
rwolf sl wl b c if(balancedness == 1), indepvar(t_a_service12) controls($x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_*) method(regress) seed(123456789) reps(100) vce(cluster respondentid) cluster(respondentid)

drop t_a_service12

// // Lee (2009) Bounds

set seed 123456789

capture noisily leebounds satisfaction_life treatment_a_service12, vce(bootstrap, reps(100)) cieffect tight(age2)
capture noisily leebounds worthwhileness_life treatment_a_service12, vce(bootstrap, reps(100)) cieffect tight(age2)
capture noisily leebounds belongingness treatment_a_service12, vce(bootstrap, reps(100)) cieffect tight(age2)
capture noisily leebounds connectedness treatment_a_service12, vce(bootstrap, reps(100)) cieffect tight(age2)

// Check In and Chat
////////////////////

// Life Satisfaction

quietly reg satisfaction_life treatment_a_service3 $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(balancedness == 1), robust
generate marker = 1 if(e(sample))

reg satisfaction_life treatment_a_service3                 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust
reg satisfaction_life treatment_a_service3 $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust

sum satisfaction_life if(marker == 1)
tab treatment_a_service3 if(marker == 1)

drop marker

// Sense of Purpose in Life

quietly reg worthwhileness_life treatment_a_service3 $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(balancedness == 1), robust
generate marker = 1 if(e(sample))

reg worthwhileness_life treatment_a_service3                 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust
reg worthwhileness_life treatment_a_service3 $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust

sum worthwhileness_life if(marker == 1)
tab treatment_a_service3 if(marker == 1)

drop marker

// Belongingness

quietly reg belongingness treatment_a_service3 $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(balancedness == 1), robust
generate marker = 1 if(e(sample))

reg belongingness treatment_a_service3                 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust
reg belongingness treatment_a_service3 $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust

sum belongingness if(marker == 1)
tab treatment_a_service3 if(marker == 1)

drop marker

// Connectedness

quietly reg connectedness treatment_a_service3 $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(balancedness == 1), robust
generate marker = 1 if(e(sample))

reg connectedness treatment_a_service3                 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust
reg connectedness treatment_a_service3 $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust

sum connectedness if(marker == 1)
tab treatment_a_service3 if(marker == 1)

drop marker

// // Multiple Hypotheses Testing

generate t_a_service3 = treatment_a_service3

rwolf sl wl b c if(balancedness == 1), indepvar(t_a_service3) controls(                $y1 $y2 $z postcode_admin_aggregated2_*) method(regress) seed(123456789) reps(100) vce(cluster respondentid) cluster(respondentid)
rwolf sl wl b c if(balancedness == 1), indepvar(t_a_service3) controls($x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_*) method(regress) seed(123456789) reps(100) vce(cluster respondentid) cluster(respondentid)

drop t_a_service3

// // Lee (2009) Bounds

set seed 123456789

capture noisily leebounds satisfaction_life treatment_a_service3, vce(bootstrap, reps(100)) cieffect tight(age2)
capture noisily leebounds worthwhileness_life treatment_a_service3, vce(bootstrap, reps(100)) cieffect tight(age2)
capture noisily leebounds belongingness treatment_a_service3, vce(bootstrap, reps(100)) cieffect tight(age2)
capture noisily leebounds connectedness treatment_a_service3, vce(bootstrap, reps(100)) cieffect tight(age2)


////////////////////////////////////////////////////////////////////////////////////////////////
// Table 6: Heterogeneous Treatment Effect Intensity (% of Task Distribution) By Type of Task //
////////////////////////////////////////////////////////////////////////////////////////////////

// Transport and Community Response
///////////////////////////////////

// Life Satisfaction

quietly reg satisfaction_life i.treatment_a_serv12_int $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(balancedness == 1), robust
generate marker = 1 if(e(sample))

reg satisfaction_life i.treatment_a_serv12_int                 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust
reg satisfaction_life i.treatment_a_serv12_int $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust

sum satisfaction_life if(marker == 1)
tab treatment_a_serv12_int if(marker == 1)

drop marker

// Sense of Purpose in Life

quietly reg worthwhileness_life i.treatment_a_serv12_int $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(balancedness == 1), robust
generate marker = 1 if(e(sample))

reg worthwhileness_life i.treatment_a_serv12_int                 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust
reg worthwhileness_life i.treatment_a_serv12_int $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust

sum worthwhileness_life if(marker == 1)
tab treatment_a_serv12_int if(marker == 1)

drop marker

// Belongingness

quietly reg belongingness i.treatment_a_serv12_int $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(balancedness == 1), robust
generate marker = 1 if(e(sample))

reg belongingness i.treatment_a_serv12_int                 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust
reg belongingness i.treatment_a_serv12_int $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust

sum belongingness if(marker == 1)
tab treatment_a_serv12_int if(marker == 1)

drop marker

// Connectedness

quietly reg connectedness i.treatment_a_serv12_int $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(balancedness == 1), robust
generate marker = 1 if(e(sample))

reg connectedness i.treatment_a_serv12_int                 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust
reg connectedness i.treatment_a_serv12_int $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust

sum connectedness if(marker == 1)
tab treatment_a_serv12_int if(marker == 1)

drop marker

// // Multiple Hypotheses Testing

tabulate treatment_a_serv12_int, generate(t_a_serv12_int_)

rwolf sl wl b c if(balancedness == 1), indepvar(t_a_serv12_int_2 t_a_serv12_int_3 t_a_serv12_int_4 t_a_serv12_int_5 t_a_serv12_int_6 t_a_serv12_int_7) controls(                $y1 $y2 $z postcode_admin_aggregated2_*) method(regress) seed(123456789) reps(100) vce(cluster respondentid) cluster(respondentid)
rwolf sl wl b c if(balancedness == 1), indepvar(t_a_serv12_int_2 t_a_serv12_int_3 t_a_serv12_int_4 t_a_serv12_int_5 t_a_serv12_int_6 t_a_serv12_int_7) controls($x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_*) method(regress) seed(123456789) reps(100) vce(cluster respondentid) cluster(respondentid)

drop t_a_serv12_int_1
drop t_a_serv12_int_2
drop t_a_serv12_int_3
drop t_a_serv12_int_4
drop t_a_serv12_int_5
drop t_a_serv12_int_6
drop t_a_serv12_int_7

// Check In and Chat
////////////////////

// Life Satisfaction

quietly reg satisfaction_life i.treatment_a_service3_int $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(balancedness == 1), robust
generate marker = 1 if(e(sample))

reg satisfaction_life i.treatment_a_service3_int                 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust
reg satisfaction_life i.treatment_a_service3_int $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust

sum satisfaction_life if(marker == 1)
tab treatment_a_service3_int if(marker == 1)

drop marker

// Sense of Purpose in Life

quietly reg worthwhileness_life i.treatment_a_service3_int $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(balancedness == 1), robust
generate marker = 1 if(e(sample))

reg worthwhileness_life i.treatment_a_service3_int                 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust
reg worthwhileness_life i.treatment_a_service3_int $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust

sum worthwhileness_life if(marker == 1)
tab treatment_a_service3_int if(marker == 1)

drop marker

// Belongingness

quietly reg belongingness i.treatment_a_service3_int $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(balancedness == 1), robust
generate marker = 1 if(e(sample))

reg belongingness i.treatment_a_service3_int                 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust
reg belongingness i.treatment_a_service3_int $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust

sum belongingness if(marker == 1)
tab treatment_a_service3_int if(marker == 1)

drop marker

// Connectedness

quietly reg connectedness i.treatment_a_service3_int $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(balancedness == 1), robust
generate marker = 1 if(e(sample))

reg connectedness i.treatment_a_service3_int                 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust
reg connectedness i.treatment_a_service3_int $x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_* if(marker == 1 & balancedness == 1), robust

sum connectedness if(marker == 1)
tab treatment_a_service3_int if(marker == 1)

drop marker

// // Multiple Hypotheses Testing

tabulate treatment_a_service3_int, generate(t_a_serv3_int)

rwolf sl wl b c if(balancedness == 1), indepvar(t_a_serv3_int2 t_a_serv3_int3 t_a_serv3_int4 t_a_serv3_int5 t_a_serv3_int6 t_a_serv3_int7) controls(                $y1 $y2 $z postcode_admin_aggregated2_*) method(regress) seed(123456789) reps(100) vce(cluster respondentid) cluster(respondentid)
rwolf sl wl b c if(balancedness == 1), indepvar(t_a_serv3_int2 t_a_serv3_int3 t_a_serv3_int4 t_a_serv3_int5 t_a_serv3_int6 t_a_serv3_int7) controls($x1 $x2 $x3 $x4 $y1 $y2 $z postcode_admin_aggregated2_*) method(regress) seed(123456789) reps(100) vce(cluster respondentid) cluster(respondentid)

drop t_a_serv3_int1
drop t_a_serv3_int2
drop t_a_serv3_int3
drop t_a_serv3_int4
drop t_a_serv3_int5
drop t_a_serv3_int6
drop t_a_serv3_int7


////////////////////////////////////////////////////////////////////////////////////////////
// Table 7: Predicting Survey Response From Having Been Treated in Administrative Records //
////////////////////////////////////////////////////////////////////////////////////////////

// Administrative Data

append using "Admin_Data_Task_Behaviour_Universe_2a.dta"
append using "Admin_Data_Task_Behaviour_Universe_2b.dta"
append using "Admin_Data_Task_Behaviour_Universe_2c.dta"
append using "Admin_Data_Task_Behaviour_Universe_2d.dta"
append using "Admin_Data_Task_Behaviour_Universe_2e.dta"

// Variables

generate admin_records_refined = 1 if(responderId != . & (neverdownloadedapp == "NULL" & number_days_duty >= 1))

generate survey_admin_data_response = .
replace survey_admin_data_response = 1 if(respondentid != . & responderId != .)
replace survey_admin_data_response = 0 if(respondentid == . | responderId == .)

generate treatment_a_2 = .
replace treatment_a_2 = 1 if(allocatedTask == 1 & acceptedTask == 1 & completedTask == 1)
replace treatment_a_2 = 0 if(allocatedTask == 0 | (allocatedTask == 1 & acceptedTask == 0 & numberTimedOutTasks > 0))
tab treatment_a_2

generate date_birth = date(dateOfBirth, "DMY")
format date_birth %td
generate year_birth = year(date_birth)
generate age_temp = 2020 - year_birth
replace age_temp = . if(age_temp < 18)

replace age2 = 1 if(age2 == . & age_temp != . & (age_temp < 16 | (age_temp >= 16 & age_temp <= 24)))
replace age2 = 2 if(age2 == . & age_temp != . & ((age_temp >= 25 & age_temp <= 34)))
replace age2 = 3 if(age2 == . & age_temp != . & ((age_temp >= 35 & age_temp <= 44)))
replace age2 = 4 if(age2 == . & age_temp != . & ((age_temp >= 45 & age_temp <= 54)))
replace age2 = 5 if(age2 == . & age_temp != . & ((age_temp >= 55 & age_temp <= 64)))
replace age2 = 6 if(age2 == . & age_temp != . & ((age_temp >= 65)))

generate postcode_admin_agg2 = substr(pafpostcode, 1, 2)
tabulate postcode_admin_agg2, generate(postcode_admin_agg2_)

// Regressions

global ttb1 "allocatedTask acceptedTask completedTask timedOutTask rejectedTask"
global ttb2 "numberAllocatedTasks numberAcceptedTask numberTasksCompleted numberTimedOutTasks numberRejectedTasks"
global ttb3 "transport communityResponse checkInAndChat"
global r "postcode_admin_agg2_*"

reg survey_admin_data_response treatment_a_2 if(admin_records_refined == 1), robust
tab survey_admin_data_response if(e(sample) == 1)
tab treatment_a_2 if(e(sample) == 1)

reg survey_admin_data_response treatment_a_2 $ttb1 $ttb2 $ttb3 $r if(admin_records_refined == 1), robust
tab survey_admin_data_response if(e(sample) == 1)
tab treatment_a_2 if(e(sample) == 1)

log close _all

